package viettravel.guiandevent.expandable;

import java.util.ArrayList;

import viettravel.DAO.ProvinceDAO;
import viettravel.DAO.RegionDAO;
import viettravel.common.Constants;
import viettravel.guiandevent.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

@SuppressLint("DefaultLocale")
public class ExpandableListAdapter extends BaseExpandableListAdapter{

	//Define attribute
	private Context mContext;
	private ArrayList<RegionExpandable> mRegionExpandableOriginalList;
	private ArrayList<RegionExpandable> mRegionResultList;
	
	public ExpandableListAdapter(Context _context, ArrayList<RegionExpandable> _originalList){
		mContext = _context;
		//mViettravelDTO = ViettravelDTO.getInstance();
		mRegionExpandableOriginalList = new ArrayList<RegionExpandable>();
		mRegionExpandableOriginalList.addAll(_originalList);
		mRegionResultList = new ArrayList<RegionExpandable>();
		mRegionResultList.addAll(_originalList);
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		RegionExpandable regionExpandable = mRegionResultList.get(groupPosition);
		ProvinceDAO provinceDAO = regionExpandable.getProvinceList().get(childPosition);
		return provinceDAO;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		  return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
			View view, ViewGroup parent) {
		RegionExpandable regionExpandable = mRegionResultList.get(groupPosition);
		ProvinceDAO provinceDAO = regionExpandable.getProvinceList().get(childPosition);
		
		if (view == null) {
			LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = layoutInflater.inflate(R.layout.layout_province_row, null);
			}
		Typeface tf = Typeface.createFromAsset(mContext.getAssets(),"fonts/utm_nyala.ttf");
		TextView provinceName = (TextView) view.findViewById(R.id.province_name);
		provinceName.setTypeface(tf);
		provinceName.setText(provinceDAO.getProvinceName().trim());
		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		RegionExpandable regionExpandable = mRegionResultList.get(groupPosition);
		ArrayList<ProvinceDAO> provinceList = regionExpandable.getProvinceList();
		return provinceList.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		RegionExpandable regionExpandable = mRegionResultList.get(groupPosition);
		return regionExpandable;
	}

	@Override
	public int getGroupCount() {
		return mRegionResultList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isLastChild,
			View view, ViewGroup parent) {
		RegionExpandable regionExpandable = mRegionResultList.get(groupPosition);
		RegionDAO regionDAO = regionExpandable.getRegionDAO();
		if (view == null) {
			LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = layoutInflater.inflate(R.layout.layout_region_row, null);
		}
		Typeface tf = Typeface.createFromAsset(mContext.getAssets(),"fonts/utm_caviar.ttf");
		TextView heading = (TextView) view.findViewById(R.id.region_name);
		heading.setTypeface(tf);
		heading.setText(regionDAO.getRegionName().trim());
		return view;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	public void filterData(String query){
		query = query.toLowerCase();
		mRegionResultList.clear();
		   
		if(query.equals("")){
			mRegionResultList.addAll(mRegionExpandableOriginalList);
		}else {
			for(RegionExpandable regionExpandable : mRegionExpandableOriginalList){
				ArrayList<ProvinceDAO> newProvinceList = new ArrayList<ProvinceDAO>();
				for(ProvinceDAO province : regionExpandable.getProvinceList()){
					if(Constants.isContain(province.getProvinceName(), query))
						newProvinceList.add(province);
				}
				if(newProvinceList.size() > 0){
					mRegionResultList.add(new RegionExpandable(regionExpandable.getRegionDAO(), newProvinceList));
				}
			}
		}
		notifyDataSetChanged();
	}
	

}
